package cn.lili.modules.promotion.serviceimpl;

import cn.lili.modules.member.client.ArticleClient;
import cn.lili.modules.page.entity.dos.Article;
import cn.lili.modules.promotion.entity.dos.BlindboxArticleRel;
import cn.lili.modules.promotion.entity.vos.BlindboxArticleRelVO;
import cn.lili.modules.promotion.mapper.BlindboxArticleRelMapper;
import cn.lili.modules.promotion.service.BlindboxArticleRelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class BlindboxArticleRelServiceImpl extends ServiceImpl<BlindboxArticleRelMapper, BlindboxArticleRel> implements BlindboxArticleRelService {

    private final ArticleClient articleClient;

    /**
     * 同意抽奖协议
     * @param extendId 用户ID
     * @param blindboxArticleRelVO 协议
     * */
    @Override
    public Boolean updateAgree(String extendId, BlindboxArticleRelVO blindboxArticleRelVO) {
        BlindboxArticleRel blindboxArticleRel = new BlindboxArticleRel();
        blindboxArticleRel.setUserId(extendId);
        blindboxArticleRel.setIsAgree(true);
        blindboxArticleRel.setArticleId(blindboxArticleRelVO.getArticleId());
        this.baseMapper.insert(blindboxArticleRel);
        return Boolean.TRUE;
    }

    /**
     * 根据协议ID查询
     * @param articleId 协议ID
     * */
    @Override
    public List<BlindboxArticleRel> getBlindboxArticleRelByarticleId(String userId,String articleId) {
        LambdaQueryWrapper<BlindboxArticleRel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BlindboxArticleRel::getUserId,userId);
        lambdaQueryWrapper.eq(BlindboxArticleRel::getArticleId,articleId);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 通过类型获取文章
     * @param extendId 用户ID
     * @param type  类型
     * */
    @Override
    public List<Article> getAllByType(String extendId, String type) {
        List<Article> list = articleClient.getAllByType(type);
        if(list!=null && list.size()>0){
            List<BlindboxArticleRel> blindboxArticleRelByarticleId = getBlindboxArticleRelByarticleId(extendId,list.get(0).getId());
            if(blindboxArticleRelByarticleId.size()>0){
                return null;
            }
            return list;
        }
        return list;
    }
}
